/*
 * example.c - Usage examples for check library
 */

#include "bounds_check.h"
#include <stdio.h>
#include <string.h>

void demonstrate_string_operations() {
    printf("=== String Operations ===\n");

    char buffer[32];

    // Safe string copy
    if (strcpy_s(buffer, sizeof(buffer), "Hello, World!") == BOUNDS_CHECK_SUCCESS) {
        printf("strcpy_s: %s\n", buffer);
    }

    // Safe string concatenation
    if (strcat_s(buffer, sizeof(buffer), " Welcome!") == BOUNDS_CHECK_SUCCESS) {
        printf("strcat_s: %s\n", buffer);
    }

    // Bounded string copy
    if (strncpy_s(buffer, sizeof(buffer), "Truncated", 5) == BOUNDS_CHECK_SUCCESS) {
        printf("strncpy_s: %s\n", buffer);
    }

    printf("\n");
}

void demonstrate_memory_operations() {
    printf("=== Memory Operations ===\n");

    char source[] = "Test data for memory operations";
    char destination[64];

    // Safe memory copy
    if (memcpy_s(destination, sizeof(destination), source, strlen(source) + 1) == BOUNDS_CHECK_SUCCESS) {
        printf("memcpy_s: %s\n", destination);
    }

    // Safe memory initialization
    char clear_buffer[16];
    if (memset_s(clear_buffer, sizeof(clear_buffer), 'X', 10) == BOUNDS_CHECK_SUCCESS) {
        printf("memset_s: ");
        for (int i = 0; i < 10; i++) {
            printf("%c", clear_buffer[i]);
        }
        printf("\n");
    }

    printf("\n");
}

void demonstrate_error_handling() {
    printf("=== Error Handling ===\n");

    char small_buffer[5];
    const char *long_string = "This string is too long for the buffer";

    // This should fail with BUFFER_TOO_SMALL
    int result = strcpy_s(small_buffer, sizeof(small_buffer), long_string);

    switch (result) {
        case BOUNDS_CHECK_SUCCESS:
            printf("Unexpected success!\n");
            break;
        case BOUNDS_CHECK_BUFFER_TOO_SMALL:
            printf("Correctly detected buffer too small\n");
            break;
        case BOUNDS_CHECK_INVALID_PARAM:
            printf("Invalid parameters detected\n");
            break;
        default:
            printf("Unknown error: %d\n", result);
            break;
    }

    // Test with NULL pointer
    result = strcpy_s(NULL, sizeof(small_buffer), "test");
    if (result == BOUNDS_CHECK_INVALID_PARAM) {
        printf("Correctly detected NULL destination\n");
    }

    printf("\n");
}

void demonstrate_utility_functions() {
    printf("=== Utility Functions ===\n");

    const char *test_string = "Hello";
    size_t length;

    // Safe string length
    if (strnlen_s(test_string, 10, &length) == BOUNDS_CHECK_SUCCESS) {
        printf("strnlen_s: String '%s' has length %zu\n", test_string, length);
    }

    // Buffer validation
    char valid_buffer[16];
    if (check_buffer(valid_buffer, sizeof(valid_buffer)) == BOUNDS_CHECK_SUCCESS) {
        printf("check_buffer: Buffer is valid\n");
    }

    printf("\n");
}

int main() {
    printf("mBoundsCheck Library Examples\n");
    printf("============================\n\n");

    demonstrate_string_operations();
    demonstrate_memory_operations();
    demonstrate_error_handling();
    demonstrate_utility_functions();

    printf("All examples completed successfully!\n");
    return 0;
}